package bean;

import json.person;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

public class function
{
    private static final String PERSISTENCE_UNIT_NAME = "MyDSTest";
    //判断是否合法
    public static boolean register_check(String name,String email,String passwd,String passwd2)
    {
        if((!passwd.equals(passwd2))||(name.length()<=0)||(passwd.length()<6)||(email.length()<=0))
        {
            return false;
        }
        return true;
    }
    //判断注册数据是否已经存在
    public static boolean ifExit(String name)
    {
        EntityManagerFactory entityManagerFactory= Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);

        EntityManager em = entityManagerFactory.createEntityManager();

        User user=em.find(User.class,name);

        em.close();

        if(user==null) return true;
        else return false;
    }
    public static User writeToUserid(person dgut,User user)
    {
        EntityManagerFactory entityManagerFactory=Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);

        EntityManager em = entityManagerFactory.createEntityManager();

        EntityTransaction t = em.getTransaction();
        System.out.println(dgut.getName());
        String id=dgut.getUsername();
        t.begin();
        User temp=em.find(User.class,user.getUsername());
        if(temp!=null){

            temp.setId(id);
            t.commit();
        }
        else
        {
            return null;
        }
        em.close();
        return temp;
    }
    public static void writeToUser(String name,String email,String passwd)
    {
        EntityManagerFactory entityManagerFactory=Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);

        EntityManager em = entityManagerFactory.createEntityManager();

        EntityTransaction t = em.getTransaction();

        try {

            User user = new User(name,email,passwd,"image/default.jpg",null);

            t.begin();

            em.persist(user);

            t.commit();


        } catch (Exception ex) {
            System.out.println("注册失败");
        } finally {
            //close the em to release any resources held up by the persistebce provider
            em.close();
        }
    }

    //判断登录信息是否存在
    public static User Login_Exit(String name,String passwd)
    {
        EntityManagerFactory entityManagerFactory=Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);

        EntityManager em = entityManagerFactory.createEntityManager();

        User user=em.find(User.class,name);
        if(user==null)System.out.println(name + "   passwd:" +passwd  + "   ");
        em.close();

        if(user!=null)
        {
            if(user.getPasswd().equals(passwd)) return user;
            else return null;
        }
        else return null;

    }

    public static User LoginDgutUser(person Person)
    {
        BufferedReader input=null;
        File file=null;
        String s1=null;
        String s2=null;
        boolean flag=false;
        User user=null;
        String id=Person.getUsername();
        try {
            file = new File("person.txt");
            input=new BufferedReader(new FileReader(file));
            while((s1=input.readLine())!=null)
            {
                s2=input.readLine();
                if(s2!=null)
                {
                    String s[]=s2.split(",");
                    if((s[4]!=null)&&(s[4].equals(id)))
                    {
                        flag=true;
                        user=new User(s[0],s[1],s[2],s[3],s[4]);
                        return user;
                    }
                }
            }
            if(!flag)
            {
                user=new User("游客",null,"123456",null,Person.getUsername());
                //PackageFunction.writeDgutToUser(user);
                return user;
            }
        }
        catch(IOException e) {System.out.println("登录失败！");}
        finally {
            try {
                input.close();
            }catch (Exception ee) {System.out.println("文件关闭失败！");}
        }
        return user;
    }

    //将第三方莞工账户写入文件
    public static void writeDgutToUser(User user)
    {
        EntityManagerFactory entityManagerFactory=Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);

        EntityManager em = entityManagerFactory.createEntityManager();

        EntityTransaction t= em.getTransaction();

        try {
            t.begin();
            em.persist(user);
            t.commit();
        } catch (Exception ex) {
            System.out.println("第三方注册失败");
        } finally {
            //close the em to release any resources held up by the persistebce provider
            em.close();
        }

    }


    public static void UploadImg(User user)
    {
        EntityManagerFactory entityManagerFactory=Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);

        EntityManager em = entityManagerFactory.createEntityManager();

        EntityTransaction t = em.getTransaction();

        User user2=em.find(User.class,user.getUsername());
        user2.setImg(user.getImg());

        try {
            t.begin();
            em.merge(user2);
            em.flush();
            t.commit();
        } catch (Exception ex) {
            System.out.println("error");
        } finally {
            //close the em to release any resources held up by the persistebce provider
            em.close();
        }


    }
}
